package com.xianda.spring;

import com.xianda.spring.interceptor.ApiHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.validation.ValidatorFactory;
import java.util.List;

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
	
	@Autowired
	private ApiHandlerInterceptor apiHandlerInterceptor;
	
	@Autowired
	private ValidatorFactory validatorFactory;

	/**
	 * 配置拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(apiHandlerInterceptor).addPathPatterns("/**");
	}

	@Override
	public Validator getValidator() {
		return new SpringValidatorAdapter(validatorFactory.getValidator());
	}
	
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//		converters.add(new Jaxb2RootElementHttpMessageConverter());
//		GsonHttpMessageConverter gsonConverter = new GsonHttpMessageConverter();
//		gsonConverter.setGson( new GsonBuilder().setDateFormat("yyyyMMddHHmmss").create());
//		converters.add(gsonConverter);
	}
}
